package com.app.biz.service.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.biz.domain.BizCityIndustrialEconomics;
import com.app.biz.domain.request.BizFocusSchedulingProductProductionRequest;
import com.app.biz.mapper.BizFocusSchedulingProductProductionFormalMapper;
import com.app.common.utils.DateUtils;
import com.app.common.utils.uuid.IdUtils;
import com.app.system.domain.BizIndustrialData;
import com.app.system.mapper.BizIndustrialDataMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.biz.mapper.BizFocusSchedulingProductProductionMapper;
import com.app.biz.domain.BizFocusSchedulingProductProduction;
import com.app.biz.service.IBizFocusSchedulingProductProductionService;

/**
 * 重点调度产品产量Service业务层处理
 *
 * @author SangGuangCan
 * @date 2023-12-29
 */
@Service
public class BizFocusSchedulingProductProductionServiceImpl implements IBizFocusSchedulingProductProductionService
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final BizFocusSchedulingProductProductionMapper bizFocusSchedulingProductProductionMapper;

    private final BizFocusSchedulingProductProductionFormalMapper bizFocusSchedulingProductProductionFormalMapper;


    private final BizIndustrialDataMapper bizIndustrialDataMapper;

    @Autowired
    public BizFocusSchedulingProductProductionServiceImpl(BizFocusSchedulingProductProductionMapper bizFocusSchedulingProductProductionMapper,
                                                          BizIndustrialDataMapper bizIndustrialDataMapper,
                                                          BizFocusSchedulingProductProductionFormalMapper bizFocusSchedulingProductProductionFormalMapper){
        this.bizFocusSchedulingProductProductionMapper = bizFocusSchedulingProductProductionMapper;
        this.bizIndustrialDataMapper = bizIndustrialDataMapper;
        this.bizFocusSchedulingProductProductionFormalMapper = bizFocusSchedulingProductProductionFormalMapper;
    }

    /**
     * 查询重点调度产品产量
     *
     * @param baseId 重点调度产品产量主键
     * @return 重点调度产品产量
     */
    @Override
    public BizFocusSchedulingProductProduction selectBizFocusSchedulingProductProductionByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionMapper.selectBizFocusSchedulingProductProductionByBaseId(baseId);
    }

    /**
     * 查询重点调度产品产量列表
     *
     * @param bizFocusSchedulingProductProduction 重点调度产品产量
     * @return 重点调度产品产量
     */
    @Override
    public List<BizFocusSchedulingProductProduction> selectBizFocusSchedulingProductProductionList(BizFocusSchedulingProductProduction bizFocusSchedulingProductProduction)
    {
        List<BizFocusSchedulingProductProduction> bizFocusSchedulingProductProductions = bizFocusSchedulingProductProductionMapper.selectBizFocusSchedulingProductProductionList(bizFocusSchedulingProductProduction);

        return bizFocusSchedulingProductProductions;
    }

    /**
     * 新增重点调度产品产量
     *
     * @param bizFocusSchedulingProductProduction 重点调度产品产量
     * @return 结果
     */
    @Override
    public int insertBizFocusSchedulingProductProduction(BizFocusSchedulingProductProduction bizFocusSchedulingProductProduction)
    {
        bizFocusSchedulingProductProduction.setBaseId(IdUtils.simpleUUID());
        bizFocusSchedulingProductProduction.setBaseCreateTime(DateUtils.getNowDate());
        return bizFocusSchedulingProductProductionMapper.insertBizFocusSchedulingProductProduction(bizFocusSchedulingProductProduction);
    }

    /**
     * 修改重点调度产品产量
     *
     * @param bizFocusSchedulingProductProduction 重点调度产品产量
     * @return 结果
     */
    @Override
    public int updateBizFocusSchedulingProductProduction(BizFocusSchedulingProductProduction bizFocusSchedulingProductProduction)
    {
        return bizFocusSchedulingProductProductionMapper.updateBizFocusSchedulingProductProduction(bizFocusSchedulingProductProduction);
    }

    /**
     * 批量删除重点调度产品产量
     *
     * @param baseIds 需要删除的重点调度产品产量主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionByBaseIds(String[] baseIds)
    {
        return bizFocusSchedulingProductProductionMapper.deleteBizFocusSchedulingProductProductionByBaseIds(baseIds);
    }

    /**
     * 删除重点调度产品产量信息
     *
     * @param baseId 重点调度产品产量主键
     * @return 结果
     */
    @Override
    public int deleteBizFocusSchedulingProductProductionByBaseId(String baseId)
    {
        return bizFocusSchedulingProductProductionMapper.deleteBizFocusSchedulingProductProductionByBaseId(baseId);
    }

    /**
     * <p>
     * 查询数据库中数据的日期(year_month字段)并进行去重复操作<br/>
     * 主要用于的是APP端左上角的日期过滤
     * </p>
     *
     * @return 日期的数组
     */
    @Override
    public List<String> getUniqueDatesFromDatabase() {
        List<String> uniqueDatesFromDatabase = bizFocusSchedulingProductProductionMapper.getUniqueDatesFromDatabase();
        if(!uniqueDatesFromDatabase.isEmpty()){
            //Mysql中去重关键字和排序不能对同一字段同时使用，这里通过代码来去重
            return uniqueDatesFromDatabase.stream().distinct().collect(Collectors.toList());
        }
        return uniqueDatesFromDatabase;
    }

    /**
     * 获取APP表格中的数据
     *
     * @param request
     * @return
     */
    @Override
    public List<BizFocusSchedulingProductProduction> getAppTableData(BizFocusSchedulingProductProductionRequest request) {
        String dates = request.getDates();
        DateTime parse = DateUtil.parse(dates, "yyyy年MM月");
        request.setDates(DateUtil.format(parse,"yyyy-MM-dd"));
        List<BizFocusSchedulingProductProduction> appTableData = bizFocusSchedulingProductProductionMapper.getAppTableData(request);

        List<BizIndustrialData> noParentNodeArgs = bizIndustrialDataMapper.selectBizIndustrialDataNoParentNode();
        Map<String, List<BizIndustrialData>> collect = noParentNodeArgs.stream().collect(Collectors.groupingBy(BizIndustrialData::getParentId));

        for(BizFocusSchedulingProductProduction item : appTableData){
            String typeBaseId = item.getTypeBaseId();
            if(collect.containsKey(typeBaseId)) {
                List<BizIndustrialData> bizIndustrialData = collect.get(typeBaseId);
                if(bizIndustrialData != null && !bizIndustrialData.isEmpty()){
                    item.setHaveLeft("1");
                }else{
                    item.setHaveLeft("0");
                }
            }else{
                item.setHaveLeft("0");
            }
        }
        return appTableData;
    }

    /**
     * @return
     */
    @Override
    public boolean transferDataInOneClick() {
        try{
            /* 查询数据库中最新的一条日期 */
//            String latestDateStr = bizFocusSchedulingProductProductionMapper.getLatestDateData();
//            BizFocusSchedulingProductProduction params = new BizFocusSchedulingProductProduction();
//            params.setYearMonth(DateUtils.parseDate(latestDateStr,"yyyy-MM-dd"));

            List<BizFocusSchedulingProductProduction> result = bizFocusSchedulingProductProductionMapper.selectBizFocusSchedulingProductProductionAllData();

            int count = 0;
            List<BizFocusSchedulingProductProduction> list = new ArrayList<>();
            for(BizFocusSchedulingProductProduction item : result){
                item.setBaseCreateTime(DateUtils.getNowDate());
                list.add(item);
                count++;
                if(count >= 100){
                    bizFocusSchedulingProductProductionFormalMapper.batchInsertBizIndustrialSectorMetricsFormal(list);
                    list.clear();
                }
            }

            if(!list.isEmpty()){
                bizFocusSchedulingProductProductionFormalMapper.batchInsertBizIndustrialSectorMetricsFormal(list);
                list.clear();
            }
            return true;
        }catch (Exception e){
            logger.error("未知错误:{}",e.getMessage());
            return false;
        }
    }

    /**
     * <p>
     *     通过父级节点查询子级节点
     * </p>
     * @param pId
     * @return
     */
    @Override
    public List<BizFocusSchedulingProductProduction> selectChNodeByParentId(String pId) {
        return bizFocusSchedulingProductProductionMapper.selectBizFocusSchedulingProductProductionByParentId(pId);
    }
}
